package com.c.ctgproject.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.MenuBeanV2
import com.c.ctgproject.utils.MyConstants

class CommonMenuAdapter(private val context: Context, private val mList: List<MenuBeanV2>) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_menu_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mNameTV.text = data.name
            Glide.with(context).load(data.imageResource).into(holder.mImageView)
            holder.mParentLL.setOnClickListener {
                val intent = Intent(context, data.activity)
                intent.putExtra("searchType", data.searchType)
                intent.putExtra(MyConstants.TITLE_NAME, data.name)
                context.startActivity(intent)
            }
            //数字统计
            if (data.count == 0 || data.count == null) {
                holder.mCountTV.visibility = View.GONE
            } else {
                holder.mCountTV.visibility = View.VISIBLE
                //if (data.count!! < 100) {
                holder.mCountTV.text = data.count.toString()
//                } else {
//                    holder.mCountTV.text = "99+"
//                }
            }
        }
    }


    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mNameTV: TextView = view.findViewById(R.id.mNameTV)
        val mImageView: ImageView = view.findViewById(R.id.mImageView)
        val mCountTV: TextView = view.findViewById(R.id.mCountTV)
        val mParentLL: LinearLayout = view.findViewById(R.id.mParentLL)
    }
}